﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using System.Collections.Specialized;
using System.Web;
using System.Web.Routing;
namespace Infrastructure.Web.ExceptionHandling
{
    [ConfigurationElementType(typeof(CustomHandlerData))]
    public class WebPresenterExceptionHandler : IExceptionHandler
    {
        private string RouteTarget = "";
        public WebPresenterExceptionHandler(NameValueCollection args)
        {
            this.RouteTarget = args["RouteTarget"];

        }
        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            var dic = new RouteValueDictionary();
            dic.Add("previuspath", HttpContext.Current.Request.Path);
            HttpContext.Current.Session[Constants.SessionNames.SupportLastException] = exception;
            var vp = RouteTable.Routes.GetVirtualPath(null, RouteTarget, dic).VirtualPath;                        
            HttpContext.Current.Response.Redirect(vp, false);            
            return exception;
        }

    }
}
